package com.tanjie.blackbean.web.log;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

import static cn.hutool.core.date.DatePattern.NORM_DATETIME_MS_PATTERN;

/**
 * @author: tanjie
 * @date: 2022-04-13
 **/
@Slf4j
public final class LogPrinter {

    private LogPrinter() {
    }

    public static final String LOG_MDC_KEY = "LOG_THREAD_ID";

    public static void print(ApiLogInfo logInfo) {
        // 打印请求相关参数
        log.info("REQUEST INFO: {}{}{}" +
                        "id:          {}{}" +
                        "time:        {}{}" +
                        "appName:     {}{}" +
                        "title:       {}{}" +
                        "uri:         {}{}" +
                        "method:      {}{}" +
                        "remoteAddr:  {}{}" +
                        "userAgent:   {}{}" +
                        "handler:     {}{}" +
                        "requestArgs: {}{}" +
                        "response:    {}{}" +
                        "timeConsuming: {}{}{}",
                "\n",
                "-------------------------------------------------------------------------------------------------------",
                "\n", logInfo.getLogThreadId(),
                "\n", DateUtil.format(new Date(), NORM_DATETIME_MS_PATTERN),
                "\n", logInfo.getAppName(),
                "\n", logInfo.getTitle(),
                "\n", logInfo.getUri(),
                "\n", logInfo.getMethod(),
                "\n", logInfo.getRemoteAddress(),
                "\n", logInfo.getUserAgent(),
                "\n", logInfo.getHandler(),
                "\n", logInfo.getRequestArgs(),
                "\n", logInfo.getResponseString(),
                "\n", logInfo.getTimeConsuming() + "ms",
                "\n",
                "-------------------------------------------------------------------------------------------------------");
    }

}
